﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SoldierArchives.BLL;

public partial class Police_Question_ZongExamination2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        
        }

    }

    //void BindSelect()
    //{
    //    List<SlectDepartment> selectList = ViewState["selectList"] as List<SlectDepartment>;

    //    dptDepartment.Items.Clear();
    //    foreach (SlectDepartment selected in selectList)
    //    {
    //        dptDepartment.Items.Add(new ListItem(selected.text, selected.value));
    //    }

    //}

    private void Bind()
    {
        for (int i = 0; i < 59; i++)
        {
            if (i < 24)
            {
                dptHours.Items.Add(new ListItem(i.ToString(), i.ToString()));
                dptHours2.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }

            dptMine.Items.Add(new ListItem(i.ToString(), i.ToString()));
            dptMine2.Items.Add(new ListItem(i.ToString(), i.ToString()));

        }
        cblDepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" (SUBSTRING(DepartmentID, 5, 3) = '000') And DepartmentID<>'1300000'");
        cblDepartmentList.DataBind();

        //if (bool.Parse(Request["Check"].ToString()))
        //{
        //    cbx1.Enabled = false;
        //    cbx2.Enabled = false;
        //}

        QuestionDataContext db = new QuestionDataContext();
        var result = (from obj in db.Associate where obj.SID == new Guid(Request["ID"]) select obj).ToList();
        if (result == null)
            return;
       
        txtStartTime.Text = result.FirstOrDefault().StartDate.Value.ToString("yyyy-MM-dd");
        txtEndTime.Text = result.FirstOrDefault().EndDate.Value.ToString("yyyy-MM-dd");

        dptHours.SelectedValue = result.FirstOrDefault().StartDate.Value.Hour.ToString();
        dptHours2.SelectedValue = result.FirstOrDefault().EndDate.Value.Hour.ToString();
        dptMine.SelectedValue = result.FirstOrDefault().StartDate.Value.Minute.ToString();
        dptMine2.SelectedValue = result.FirstOrDefault().EndDate.Value.Minute.ToString();

        foreach (Associate ass in result)
        {
            foreach (ListItem item in cblDepartmentList.Items)
            {
                if ((ass.DepartmentID.Substring(0,4)+"000") == item.Value)
                {
                    item.Selected = true;
                }
            }
        }

    }

    //private void BindDepartmentoffer()
    //{
    //    DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" (SUBSTRING(DepartmentID, 5, 3) = '000') ");
    //    DepartmentList.DataBind();
    //    string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
    //    if (!DepartmentID.Substring(2, 3).Equals("000"))
    //    {
    //        DepartmentList.SelectedValue = DepartmentID.Substring(0, 4) + "000";
    //        this.DepartmentList.Enabled = false;
    //    }


    //    PermissionCategoryList_SelectedIndexChanged(this, new EventArgs());
    //}

    //private void BindDepartment()
    //{
    //    DepartmentDataContext pt = new DepartmentDataContext();

    //    Soldier soldier = new Soldier();

    //    List<string> departmentids = soldier.GetHaveSoldierDepartmentIDs("DepartmentID like '" + DepartmentList.SelectedValue.Substring(0, 4) + "%' And IsDelete=0");

    //    var result2 = from p in pt.Account_Departments where departmentids.Contains(p.DepartmentID) select p;
    //    this.lstDepartment.DataSource = result2;
    //    this.lstDepartment.DataTextField = "FullName";
    //    this.lstDepartment.DataValueField = "DepartmentID";
    //    this.lstDepartment.DataBind();
    //}

    //protected void dptDepartment_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    if (dptDepartment.SelectedValue.Equals("00"))
    //        return;

    //    string strWhere = string.Empty;
    //    if (dptDepartment.SelectedValue.Trim().Equals("11"))
    //    {
    //        strWhere = " 1=1 and IsDelete=0";
    //    }
    //    else
    //    {

    //        strWhere = "Left(DepartmentID,4)='" + dptDepartment.SelectedValue.Substring(0, 4) + "' and IsDelete=0 ";

    //    }

    //    Soldier soldier = new Soldier();

    //    List<string> departmentids = soldier.GetHaveSoldierDepartmentIDs(strWhere);
    //    List<Yipusoft.Account.Model.DepartmentInfo> departmentlist = Yipusoft.Account.BLL.Department.GetList("1=1 And Right(DepartmentID,3)<>'000'");
    //    var pp = from p in departmentlist where departmentids.Contains(p.DepartmentID) select p;
    //    List<Yipusoft.Account.Model.DepartmentInfo> list = pp.ToList();
    //    List<MyDepartmentInfo> myList = new List<MyDepartmentInfo>();
    //    foreach (Yipusoft.Account.Model.DepartmentInfo info in list)
    //    {
    //        myList.Add(new MyDepartmentInfo() { FullName = info.FullName, DepartmentID = info.DepartmentID });
    //    }
    //    ViewState["Department"] = myList;
    //    this.lstDepartment.DataSource = list;
    //    this.lstDepartment.DataTextField = "FullName";
    //    this.lstDepartment.DataValueField = "DepartmentID";
    //    this.lstDepartment.DataBind();


    //}
    //protected void btnRemove_Click(object sender, EventArgs e)
    //{
    //    if (ViewState["Department"] == null)
    //        return;
    //    List<MyDepartmentInfo> list = ViewState["Department"] as List<MyDepartmentInfo>;

    //    foreach (ListItem item in lstDepartment.Items)
    //    {
    //        if (item.Selected)
    //        {
    //            MyDepartmentInfo info = list.Where(o => o.DepartmentID == item.Value).FirstOrDefault();
    //            list.Remove(info);
    //        }
    //    }


    //    ViewState["Department"] = list;
    //    this.lstDepartment.DataSource = list;
    //    this.lstDepartment.DataTextField = "FullName";
    //    this.lstDepartment.DataValueField = "DepartmentID";
    //    this.lstDepartment.DataBind();
    //}

    //[Serializable]
    //public class MyDepartmentInfo
    //{
    //    public string FullName
    //    {
    //        set;
    //        get;
    //    }

    //    public string DepartmentID
    //    {
    //        set;
    //        get;
    //    }
    //}
    protected void btnExamination_Click(object sender, EventArgs e)
    {
        
        

        DateTime St = DateTime.Parse(txtStartTime.Text + " " + dptHours.SelectedValue + ":" + dptMine.SelectedValue + ":00");
        DateTime Et = DateTime.Parse(txtEndTime.Text + " " + dptHours2.SelectedValue + ":" + dptMine2.SelectedValue + ":00");

        //try
        //{
            string userID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID;
            string DepartmentID=Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
            string[] strArry = Request["ID"].Split(new string[] { "@" }, StringSplitOptions.RemoveEmptyEntries);
            QuestionDataContext db = new QuestionDataContext();

            var oo = (from o in db.Associate where o.SID == new Guid(Request["ID"]) select o).ToList();
            db.Associate.DeleteAllOnSubmit(oo);
            db.SubmitChanges();
            //foreach (string id in strArry)
            //{
                foreach (ListItem item in cblDepartmentList.Items)
                {

                    if (item.Selected)
                    {
                        Associate ass = new Associate();
                        ass.ID = Guid.NewGuid();
                        ass.CreateDate = DateTime.Now;
                        ass.DepartmentID = item.Value.Substring(0, 4) + DepartmentID.Substring(4, 3);
                        ass.Flag = -1;
                        ass.SID = new Guid(Request["ID"]);
                        ass.IsLevel = 0;
                        ass.StartDate = St;
                        ass.EndDate = Et;
                        db.Associate.InsertOnSubmit(ass);
                        
                    }

                }

                db.SubmitChanges();
                lblPrompt.Text = "操作已成功";
                this.btnExamination.Enabled = false;
          //  }
        //}
        //catch (Exception ex)
        //{
        //    lblPrompt.Text = ex.ToString();

        //}

    }
    //protected void PermissionCategoryList_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    BindDepartment();
    //}
    //protected void btnAddDepartment_Click(object sender, EventArgs e)
    //{
    //    List<SlectDepartment> selectList = ViewState["selectList"] as List<SlectDepartment>;
    //    foreach (ListItem Item in lstDepartment.Items)
    //    {
    //        if (Item.Selected)
    //        {
    //            int count = selectList.Where(or => or.value == Item.Value).Count();
    //            if (count == 0)
    //            {
    //                selectList.Add(new SlectDepartment() { text = Item.Text, value = Item.Value });
    //            }
    //        }
    //    }
    //    ViewState["selectList"] = selectList;
    //    BindSelect();
    //}
    //protected void btnAddDepartmentAll_Click(object sender, EventArgs e)
    //{
    //    List<SlectDepartment> selectList = ViewState["selectList"] as List<SlectDepartment>;
    //    foreach (ListItem Item in lstDepartment.Items)
    //    {
    //        int count = selectList.Where(or => or.value == Item.Value).Count();
    //        if (count == 0)
    //        {
    //            selectList.Add(new SlectDepartment() { text = Item.Text, value = Item.Value });
    //        }
    //    }
    //    ViewState["selectList"] = selectList;
    //    BindSelect();
    //}

    //[Serializable]
    //public class SlectDepartment
    //{
    //    public string text;
    //    public string value;
    //}
    //protected void ButtonRemove_Click(object sender, EventArgs e)
    //{
    //    List<SlectDepartment> selectList = ViewState["selectList"] as List<SlectDepartment>;
    //    foreach (ListItem Item in dptDepartment.Items)
    //    {
    //        if (Item.Selected)
    //        {
    //            if (selectList.Where(or => or.value == Item.Value).Count() > 0)
    //            {
    //                selectList.Remove(selectList.Where(or => or.value == Item.Value).Single());
    //            }
    //        }
    //    }
    //    ViewState["selectList"] = selectList;
    //    BindSelect();
    //}
    //protected void btnClear_Click(object sender, EventArgs e)
    //{
    //    List<SlectDepartment> selectList = ViewState["selectList"] as List<SlectDepartment>;
    //    selectList.Clear();
    //    ViewState["selectList"] = selectList;
    //    BindSelect();
    //}
}
